/* 
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.samples.view;

import com.google.android.samples.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.view.View;
import android.widget.Button;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface;


/**
 * Demonstrates the use of progress dialogs.
 */
public class ProgressBar3 extends Activity {
    private ProgressDialog mDialog;
    private int mProgress;
    private boolean mCancelled;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == PROGRESS && !mCancelled) {
                mProgress += 50;
                if (mProgress > 10000) {
                    mProgress = 0;
                }
                mDialog.setProgress(mProgress);
                sendMessageDelayed(obtainMessage(PROGRESS), 50);
            }
        }
    };
    private static final int PROGRESS = 0xDEADBEEF;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.progressbar_3);

        Button button = (Button) findViewById(R.id.showProgress);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mCancelled = false;
                mProgress = 0;
                OnCancelListener cancelListener = new OnCancelListener() {
                    public void onCancel(DialogInterface dialog) {
                        mCancelled = true;
                        //todo: remove before submiting
//                        Log.v("ProgressBarTest", "Canceled the progress bar.");
                    }
                };

                mDialog = ProgressDialog.show(ProgressBar3.this,
                        "Doing Stuff", "Please wait while loading...", false,
                        true, cancelListener);

                mHandler.sendMessage(mHandler.obtainMessage(PROGRESS));
            }
        });

        button = (Button) findViewById(R.id.showIndeterminate);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ProgressDialog.show(ProgressBar3.this,
                        "Indeterminate", "Please wait while loading...", true,
                        true);
            }
        });

        button = (Button) findViewById(R.id.showIndeterminateNoTitle);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ProgressDialog.show(ProgressBar3.this,
                        null, "Please wait while loading...", true, true);
            }
        });
    }
}
